<?php
/**
 * Bayon helper
 * 
 * @author Prak Sophy, sophy.prak@gmail.com
 * 
 */
class BayonHelper extends Helper {
	
/**
 * Include helpers
 *
 * @var array
 */	
	public $helpers = array( 'Html','Session', 'Js' );
	
	public function bayonFilter($ret){
		$ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
	  	$ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
	  	$ret = preg_replace("/@(\w+)/", "@<a href=\"http://www.bayonpage.com/\\1\" target=\"_blank\">\\1</a>", $ret);
	  	$ret = preg_replace("/#(\w+)/", "<a href=\"http://search.bayonpage.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $ret);
		return $ret;
	}
	
	private function getUser(){
		if( $this->Session->check('Auth.User') ){
			return $this->Session->read('Auth.User');
		}else{
			return false;
		}
		
	}
/**
 * If Guest
 * 
 */    
	public function isGuest(){
    	if(!$this->Session->read('Auth.User')){
    		return true;
    	}else{
    		return false;
    	}
    	
    }
    
	public function isLogging(){
		if($this->getUser()){
			return true;
		}else{
			return false;
		}
	}
	
	public function username(){
		$user = $this->getUser();
		if( $user ){
			return $user['username'];
		}else{
			return false;
		}
	}
	
	public function userId(){
		$user = $this->getUser();
		if( $user ){
			return $user['id'];
		}else{
			return false;
		}
	}

/**
 * Share for social media
 *
 */
    public function share($title, $link, $option = array()){
        
        $li = $this->Html->tag('li', $this->Html->image('share/facebook.png'), array('class' => 'facebook'));
        $li .= $this->Html->tag('li', $this->Html->image('share/twitter.png'), array('class' => 'twitter'));
        $li .= $this->Html->tag('li', $this->Html->image('share/linkedin.png'), array('class' => 'linkedin'));
        $li .= $this->Html->tag('li', $this->Html->image('share/digg.png'), array('class' => 'digg'));

        return $this->Html->div('share-box', $this->Html->tag('ul', $li));
    }
	
}
?>